package com.pet.server.ext.action.request;

import com.pet.server.ext.ActionBase;
import com.pet.server.ext.ApiError;
import com.pet.server.ext.Result;
import com.pet.server.ext.constants.BaseAttribute;
import com.pet.server.ext.data.Page;
import com.pet.server.ext.data.RequestInfo;
import com.pet.server.ext.data.UserInfo;
import com.pet.server.ext.service.UserService;
import com.pet.server.ext.util.NumberUtil;
import com.pet.server.ext.util.Parameters;
import com.pet.server.ext.util.StringUtil;

public class Apply extends ActionBase{


	public Integer request_id,answer_id;
	public Apply(){
		 needCheckAuth =true;
	}
	
	@Override
	public void initParameters(Parameters params) throws ApiError {
		request_id = NumberUtil.getInteger(params.getFieldValue("request_id"));
		answer_id = NumberUtil.getInteger(params.getFieldValue("answer_id"));
		if(StringUtil.isEmpty(request_id,answer_id)){
			throw ApiError.Type.INVALID_PARAM.toException();
		}
	}
	
	@Override
	public Result invokeService() throws ApiError {
		UserService userService = new UserService();
		RequestInfo r =  userService.findRequestInfo(request_id);
		if(r==null||StringUtil.equals(BaseAttribute.LOGIC_TRUE, r.state)||!StringUtil.equals(r.user_id, user.user_id)){
			throw ApiError.Type.LOGIC_ERROR.toException("提问信息已经采纳或者不存在!");
		}
		UserInfo u = userService.findUserInfoById(r.user_id);
		if(r.score>u.score){
			throw ApiError.Type.LOGIC_ERROR.toException("您的积分不足!");
		}
		userService.updateRequestInfoByAnswerId(request_id, answer_id);
		Result result = new Result();
	    return result;
	}



}
